package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.lab.LabRepetition;
import com.ysd.lis.entity.lab.LabRepetitionDetail;
import com.ysd.lis.entity.lab.LabReqdetail;
import com.ysd.lis.mapper.lab.LabRepetitionDetailMapper;
import com.ysd.lis.mapper.lab.LabRepetitionMapper;
import com.ysd.lis.request.lab.LabRepetitionDto;
import com.ysd.lis.request.lab.LabRepetitionVo;
import com.ysd.lis.service.lab.LabRepetitionDetailService;
import com.ysd.lis.service.lab.LabRepetitionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 复查记录 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2024-05-07
 */
@Service
public class LabRepetitionServiceImpl extends ServiceImpl<LabRepetitionMapper, LabRepetition> implements LabRepetitionService {

    @Autowired
    LabRepetitionMapper labRepetitionMapper;
    @Autowired
    LabRepetitionDetailService labRepetitionDetailService;

    @Override
    public Result addRepetition(LabRepetitionDto labRepetitionDto) {
        LabRepetition labRepetition = new LabRepetition();
        BeanUtils.copyProperties(labRepetitionDto, labRepetition);
        labRepetitionMapper.insert(labRepetition);
        if (StringUtils.isNotBlank(labRepetition.getId())) {
            List<LabRepetitionDetail> detail = labRepetitionDto.getDetail();
            List<LabRepetitionDetail> updatedDetails = detail.stream()
                    .peek(detailItem -> detailItem.setRepetitionId(labRepetition.getId()))
                    .collect(Collectors.toList());
            boolean b = labRepetitionDetailService.saveBatch(updatedDetails);
            if (b) {
                return Result.succ("复查新增成功");

            }
        }
        return Result.fail("复查新增失败");
    }

    @Override
    public Result getRepetitionList(String mainInfoId) {

        MPJLambdaWrapper<LabRepetition> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabRepetitionVo.class);
        queryWrapper.selectAs("rep", SysUser::getRealname, LabRepetitionVo::getRepdoctorrealname);
        queryWrapper.selectCollection(LabRepetitionDetail.class,LabRepetitionVo::getRepetitionDetails);
        if (StringUtils.isBlank(mainInfoId)) {
            return Result.fail("请选择上机记录");
        }else {
            queryWrapper.eq(LabRepetitionVo::getMaininfoId, mainInfoId);
            queryWrapper.leftJoin(SysUser.class, "rep", SysUser::getId, LabRepetitionVo::getRepDoctor);
            queryWrapper.leftJoin(LabRepetitionDetail.class, LabRepetitionDetail::getRepetitionId, LabRepetitionVo::getId);
            queryWrapper.eq(LabRepetition::getDelFlag, 0);
            List<LabRepetitionVo> labRepetitionVos = labRepetitionMapper.selectJoinList(LabRepetitionVo.class, queryWrapper);
            return Result.succ(1, "查询成功", labRepetitionVos);
        }
    }
}
